package com.acme.bankapp.service.bank;

import java.util.Arrays;

import com.acme.bankapp.service.bank.exception.NotEnoughFundsException;
import com.acme.bankapp.service.bank.exception.OverDraftLimitExceededException;
import com.acme.bankapp.test.TestApplication;

public class BankApplication {

	private static final String KEY_FOR_SERIALIZATION_FILE = "-loadbank";

	private static final String KEY_FOR_SERVER = "-server";

	public static final double MAX_BALANCE = 100;

	enum TypeOperation {
		DEPOSIT_OF_FUNDS,
		WITHDRAWAL_OF_FUNDS
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Bank someBank = null;

		int index =	Arrays.binarySearch(args, KEY_FOR_SERIALIZATION_FILE);
		if (index >= 0) {
			someBank = BankService.readBank();
		} else {
			someBank = TestApplication.getTestBank();
			TestApplication.fillTestBank(someBank);
		}

		index = Arrays.binarySearch(args, KEY_FOR_SERVER);
		if (index >= 0) {
			BankServer.startServer(someBank);
		}

		BankService.printBalance(someBank);
		BankService.printMaximumAmountToWithdraw(someBank);
	}

	public static void modifyBank(Bank bank, int clientId, int accountId, TypeOperation type, double diffBalance) {
		try {
			switch (type) {
			case DEPOSIT_OF_FUNDS:
				bank.getClients()[clientId].getAccounts()[accountId].deposit(diffBalance);
				break;
			case WITHDRAWAL_OF_FUNDS:
				bank.getClients()[clientId].getAccounts()[accountId].withdraw(diffBalance);
			}
		} catch (IllegalArgumentException exception) {
			System.out.println(exception.getMessage());
		} catch (OverDraftLimitExceededException exception) {
			System.out.println(exception.getMessage());
		} catch (NotEnoughFundsException exception) {
			System.out.println(exception.getMessage());
		}
	}
}
